/* eslint-disable prettier/prettier */
import { contextBridge } from 'electron'
import { electronAPI } from '@electron-toolkit/preload'
import { get, get2, ps } from './scripts/about'
import {
  loading,
  openBrowser,
  errorMsg,
  errorMsg2,
  successMsg,
  successMsg2,
  warnMsg,
  defaultLng,
  winOpt,
  switchLng,
  getLng
} from './scripts/common'
import { initMigPath, openDirDialog, formSetRef } from './scripts/setting'
import { nodes, navTo } from './scripts/sidebar'
import {
  showReason,
  startOrStop,
  queryBtns,
  changeQueryStatus,
  openFileDialog,
  formRef,
  shutdownAll,
  wslList,
  vars,
  getWsls,
  renameFormRef,
  copyFormRef,
  initDatas,
  getDatasByTimer,
  handleNameValidate,
  handleAfterClose,
  handleImportModalOk,
  handleImportModalCancel,
  handleReNameModalOk,
  handleReNameModalCancel,
  handleCopyModalOk,
  handleCopyModalCancel,
  clickMenuItem
} from './scripts/wsl'

// Custom APIs for renderer
const api = {
  about: { get, get2, ps },
  common: { loading, openBrowser, errorMsg, errorMsg2, successMsg, successMsg2, warnMsg, defaultLng, winOpt, switchLng, getLng },
  setting: { initMigPath, openDirDialog, formSetRef },
  sidebar: { nodes, navTo },
  wsl: {
    showReason,
    startOrStop,
    queryBtns,
    changeQueryStatus,
    openFileDialog,
    formRef,
    shutdownAll,
    wslList,
    vars,
    getWsls,
    renameFormRef,
    copyFormRef,
    initDatas,
    getDatasByTimer,
    handleNameValidate,
    handleAfterClose,
    handleImportModalOk,
    handleImportModalCancel,
    handleReNameModalOk,
    handleReNameModalCancel,
    handleCopyModalOk,
    handleCopyModalCancel,
    clickMenuItem
  }
}

// Use `contextBridge` APIs to expose Electron APIs to
// renderer only if context isolation is enabled, otherwise
// just add to the DOM global.
if (process.contextIsolated) {
  try {
    contextBridge.exposeInMainWorld('electron', electronAPI)
    contextBridge.exposeInMainWorld('api', api)
  } catch (error) {
    console.error(error)
  }
} else {
  window.electron = electronAPI
  window.api = api
}
